package com.zc.inkdemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    String[] permissionparams = new String[]{"android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.ACCESS_NETWORK_STATE",
            "android.permission.READ_PHONE_STATE",
            "android.permission.CALL_PHONE",
            "android.permission.ACCESS_WIFI_STATE",
            "android.permission.RECORD_AUDIO",
            "android.permission.CAMERA",
            "android.permission.RECORD_AUDIO",
            "android.permission.INTERNET"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(Build.VERSION.SDK_INT>=23 && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, permissionparams, 1);
        } else{
//            init();
        }
        Button mButton = findViewById(R.id.btn);
        File file = new File(Environment.getExternalStorageDirectory() + "/Download/1.pdf");
        if (file.exists()) {
            Toast.makeText(this, "文件存在", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
        }
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String absolutePath = Environment.getExternalStorageDirectory() + "/Download/1.pdf";
                String absolutePath1 = new File(absolutePath).getAbsolutePath();
                Intent intent = new Intent(MainActivity.this, OpenFileActivity.class);
                intent.putExtra("filePath1", absolutePath1);
                intent.putExtra("index", -1);
                intent.putExtra("type", Constant.TYPE);
                startActivity(intent);
            }
        });
    }

    public void openFile(String fileName) {
        Intent intent = new Intent("android.intent.action.StartEIOffice_1");
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.putExtra("File_Name", fileName);
        intent.putExtra("File_Path", fileName);
        intent.putExtra("isNew", "false");
        intent.putExtra("Start_Type", "writeedit");
        intent.putExtra("Revise_Status", true);
        intent.putExtra("User_Name", "admin");
        intent.putExtra("IS_OA", true);
        intent.putExtra("ID_SUBMIT", "提交");
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            String text = "onActivityResult:OpReturn=" + data.getStringExtra("OpReturn")
                    + "UriReturn = " + data.getStringExtra("UriReturn")
                    + "Unmodified=" + data.getStringExtra("Unmodified")
                    + "DocStatus=" + data.getStringExtra("DocStatus")
                    + "SignStatus=" + data.getStringExtra("SignStatus");
            Log.e("text = ", text);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
